<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2018/10/20
 * Time: 19:47
 */
header("content-type:text/html;charset=utf-8");
require COMMON_PATH.'logTool.class.php';
require COMMON_PATH.'message.class.php';
class OrderAction extends Action
{
    //预约首页
    public function index(){
        $user = isset($_SESSION['user'])?$_SESSION['user']:'';
        logTool::logCat('session: '.$user);
        if (empty($user)){
            $this->error("请先登录",U("index/login"));
        }
        $order = M('order_list');
        $condition = array('is_delete' => 1);
        $data = $order->where($condition)->select();
        if ($data){
            $this->assign('order_list',$data);
            $this->display();
        }
    }

    //展示待处理预约页面
    public function undoPage(){
        $order = M('order_list');
        $condition = array(
            'is_delete' => 1,
            'status' => 0,
            'agree' => 1
        );
        $data = $order->where($condition)->select();
        $this->assign('order_list',$data);
        $this->display('Order:index');
    }

    //展示已处理预约页面
    public function donePage(){
        $order = M('order_list');
        $condition = array(
            'is_delete' => 1,
            'status' => 1,
            'agree' => 1
        );
        $data = $order->where($condition)->select();
        $this->assign('order_list',$data);
        $this->display('Order:index');
    }

    //查询所有订单
    public function getAllOrder(){
        $back = message::fail('查询所有订单失败');
        $order = M('order_list');
        $data = $order->select();
        $this->assign('order_list',$data);
        $this->display();
    }

    //查询所有未处理订单
    public function getAllUndoOrder(){
        $back = message::fail('查询未处理订单失败');
        $order = M('order_list');
        $data = $order->where('status = 0')->select();
        if ($data){
            $back = message::success('查询成功',$data);
        }
        logTool::logCat('查询未处理订单返回信息: '.$back);
        echo $back;
        return;
    }

    //查询所有已处理订单
    public function getAllDoneOrder(){
        $back = message::fail('查询已处理订单失败');
        $order = M('order_list');
        $data = $order->where('status = 1')->select();
        if ($data){
            $back = message::success('查询成功',$data);
        }
        logTool::logCat('查询已处理订单返回信息: '.$back);
        echo $back;
        return;
    }

    //预处理订单
    public function preDoneOrder($id){
        $back = message::fail('未查询到有效信息');
        cookie('order_id',$id);
        $order = M('order_list');
        $data = $order->where("id = {$id}")->find();
        if ($data) {
            $this->assign('data', $data);
            $content = $this->fetch();
            $back = message::success('查询成功',$content);
        }
        echo $back;
    }

    //处理订单
    public function doneOrder(){
        $back = message::fail('订单处理操作失败');
        $orderId = $_REQUEST['order_id'];
        $done_person = $_REQUEST['person'];
        $order = M('order_list');
        $data = array(
            'done_date' => time(),
            'done_person' => $done_person,
            'status' => 1
        );
        if ($order->where("id={$orderId}")->save($data)){
            $back = message::success('订单处理成功');
        }
        logTool::logCat('处理订单返回信息: '.$back);
        echo $back;
        return;
    }

    //删除订单
    public function deleteOrder($orderId){
        $back = message::fail('订单删除操作失败');
        $order = M('order_list');
        $data = array(
            'done_date' => time(),
            'is_delete' => 0
        );
        if ($order->where("id={$orderId}")->save($data)){
            $back = message::success('订单删除成功');
        }
        logTool::logCat('处理删除返回信息: '.$back);
        echo $back;
        return;
    }

    //搜索订单
    public function searchOrder(){
        $back = message::fail('搜索失败');
        //获取传输搜索条件
        $status = isset($_REQUEST['status'])?$_REQUEST['status']:'';
        $start_date = isset($_REQUEST['start_date'])?strtotime($_REQUEST['start_date'].trim()):'';
        $end_date = isset($_REQUEST['end_date'])?strtotime($_REQUEST['end_date'].trim()):'';
        $referrer = isset($_REQUEST['referrer'])?$_REQUEST['referrer'].trim():'';
        $order = M('order_list');
        $condition = array();
        $condition['is_delete'] = 1;
        $data = null;
        if ($status == 0 || $status == 1){
            $condition['status'] = $status;
        }
        if (!empty($referrer))
            $condition['referrer'] = $referrer;
        if (!empty($start_date) && !empty($end_date)){
            $where = "order_date between {$start_date} and {$end_date}";
            $data = $order->where($condition)->where($where)->select();
        } else {
            $data = $order->where($condition)->select();
        }
        logTool::logCat('搜索订单SQL: '.$order->getLastSql());
        if ($data){
            $this->assign('order_list',$data);
            $content = $this->fetch();
            $back = message::success('查询成功',$content);
        }
        logTool::logCat('搜索订单返回信息: '.$back);
        echo $back;
    }

    //查看详情
    public function getOrderInfo($id){
        $back = message::fail('未查询到有效信息');
        cookie('order_id',$id);
        $order = M('order_list');
        $data = $order->where("id = {$id}")->find();
        if ($data) {
            $this->assign('data', $data);
            $content = $this->fetch();
            $back = message::success('查询成功',$content);
        }
        echo $back;
    }

    //跳转订单页面接口
    public function backToOrder(){
        $back = message::fail('查询所有订单失败');
        $order = M('order_list');
        $data = $order->select();
        if ($data){
            $this->assign('order_list',$data);
            $content = $this->fetch();
            $back = message::success('查询成功',$content);
        }
    }

    //自动刷新
    public function refresh(){
        $back = message::fail('搜索失败');
        $order = M('order_list');
        $condition = array(
            'is_delete' => 1,
            'agree' => 1,
            'status' => 0
        );
        $data = $order->where($condition)->select();
        logTool::logCat('搜索订单SQL: '.$order->getLastSql());
        if ($data){
            $this->assign('order_list',$data);
            $content = $this->fetch('searchorder');
            $back = message::success('查询成功',$content);
        }
        logTool::logCat('搜索订单返回信息: '.$back);
        echo $back;
    }
}